package com.mini.controller;

import com.mini.config.CheckLogin;
import com.mini.model.BusinessBaseInfo;
import com.mini.model.BusinessCity;
import com.mini.repository.BusinessCityRepository;
import com.mini.service.BusinessCityService;
import com.mini.vo.BusinessResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xyf
 *
 */
@RestController
@RequestMapping("/business/city")
@Api(tags = "省份城市管理接口")
@Slf4j
public class BusinessCityController extends BaseController {


    @Autowired
    private BusinessCityService businessCityService;



    @CheckLogin
    @GetMapping("/getProvinceList")
    @ApiOperation(value = "查询省列表", notes = "查询省列表")
    public BusinessResult<List<BusinessCity>> getProvinceList(@ApiParam(value = "token", required = true) @RequestHeader String Authorization){

        List<BusinessCity> relist = businessCityService.getProvinceList();
        return BusinessResult.createSuccessInstance(relist);

    }

    @CheckLogin
    @GetMapping("/getCityListByCode")
    @ApiOperation(value = "根据省code查询市列表", notes = "根据省code查询市列表")
    public BusinessResult<List<BusinessCity>> getCityListByCode(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                              @ApiParam(value = "code",required = true)@RequestParam String code){

        List<BusinessCity> relist = businessCityService.findByPcode(code);
        relist.forEach(businessCity -> {
            String name = businessCity.getName();
            if(name.equals("市辖区")){
                BusinessCity repository = businessCityService.findByCode(businessCity.getPcode());
                businessCity.setShowName(repository.getShowName());
                businessCity.setName(repository.getName());
            }
        });
        return BusinessResult.createSuccessInstance(relist);

    }





}

